package com.scau.yangsuiyu.estore.controller.myComponent;

import com.scau.yangsuiyu.estore.controller.buyer.EditDeliveryAddressPane;
import com.scau.yangsuiyu.estore.controller.buyer.manageBasicInfo.InfoDeliveryAddressPane;
import com.scau.yangsuiyu.estore.dababases.dao.BuyerDao;
import com.scau.yangsuiyu.estore.dababases.dao.DeliveryAddressDao;
import com.scau.yangsuiyu.estore.dababases.entity.DeliveryAddress;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.layout.HBox;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * @author suiyu_yang
 * @description 收货地址条目的Controller
 * @date 2019-06-09 10:23
 * @email suiyu_yang@163.com
 */

public class DeliveryAddressHBox extends HBox implements Initializable {
    private DeliveryAddress deliveryAddress;
    private InfoDeliveryAddressPane infoDeliveryAddressPane;

    @FXML
    private Label nameLabel;

    @FXML
    private Label addressLabel;

    @FXML
    private Label zipcodeLabel;

    @FXML
    private Label phoneLabel;

    @FXML
    private HBox operateHBox;

    @FXML
    private Button changeDefaultButton;

    public DeliveryAddressHBox(DeliveryAddress deliveryAddress) {
        this.deliveryAddress = deliveryAddress;
        loadFXML();
    }

    private void loadFXML() {
        FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/myComponent/DeliveryAddressHBox.fxml"));
        fxmlLoader.setController(this);
        fxmlLoader.setRoot(this);
        try {
            fxmlLoader.load();

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        updateAddress();
    }

    @FXML
    private void changeDefaultClick() {
        deliveryAddress.getBuyer().setDefaultDeliveryAddress(deliveryAddress);
        new BuyerDao().update(deliveryAddress.getBuyer());
        new Alert(Alert.AlertType.INFORMATION, "设置成功！").showAndWait();
        updateAddress();
    }

    @FXML
    private void deleteAddressClick() {
        new DeliveryAddressDao().delete(deliveryAddress);
        new BuyerDao().update(deliveryAddress.getBuyer());
        new Alert(Alert.AlertType.INFORMATION, "删除成功！").showAndWait();
        updateAddress();
    }

    @FXML
    private void modifyAddressClick() {
        EditDeliveryAddressPane editDeliveryAddressPane = new EditDeliveryAddressPane(this);
        editDeliveryAddressPane.showPane();
    }


    /**
     * 判断该收货地址是否为默认收货地址
     *
     * @return 是则返回true, 否则返回false
     */
    private boolean ifAddressDefault() {
        DeliveryAddress defaultAddress = deliveryAddress.getBuyer().getDefaultDeliveryAddress();

        if (defaultAddress.getId() == deliveryAddress.getId()) {
            return true;
        }
        return false;
    }

    public void updateAddress() {
        nameLabel.setText(deliveryAddress.getName());
        addressLabel.setText(deliveryAddress.getAddress());
        zipcodeLabel.setText(deliveryAddress.getZipcode());
        phoneLabel.setText(deliveryAddress.getPhone());

        if (ifAddressDefault()) {
            changeDefaultButton.setText("默认地址");
            changeDefaultButton.setDisable(true);
        } else {
            changeDefaultButton.setText("设为默认");
            changeDefaultButton.setDisable(false);
        }

        if (infoDeliveryAddressPane != null) {
            //更新主界面
            infoDeliveryAddressPane.updateAddress();
        }
    }

    public DeliveryAddress getDeliveryAddress() {
        return deliveryAddress;
    }

    public void setInfoDeliveryAddressPane(InfoDeliveryAddressPane infoDeliveryAddressPane) {
        this.infoDeliveryAddressPane = infoDeliveryAddressPane;
    }
}
